﻿using EMS.system.Entityies.BaseData;
using EMS.system.IEntityAppService.BaseData.IAccountService;
using EMS.system.IEntityAppService.BaseData.IAccountService.AccountDto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace EMS.system.EntityAppService.BaseData
{
    public class AccountAppService:ApplicationService, IAccountAppService
    {
        private readonly IRepository<Account> _repository;

        public AccountAppService(IRepository<Account> repository)
        {
            _repository = repository;
        }

        /// <summary>
        /// 用户新增
        /// </summary>
        /// <param name="addDto"></param>
        /// <returns></returns>
        public async Task<int> AccountAdd(AccountAddDto addDto)
        {
            Account account = await _repository.InsertAsync(ObjectMapper.Map<AccountAddDto, Account>(addDto));
            return account == null? 0 : 1;
        }

        /// <summary>
        /// 显示用户
        /// </summary>
        /// <param name="name"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<PagedResult> AccountRoleList(string? name,int page,int limit)
        {
            PagedResult result = new PagedResult();
            List<Account> accounts = await _repository.GetListAsync(x => (string.IsNullOrEmpty(name) || x.AccountName.Contains(name)));
            result.CurrentPage = page;
            result.PageSize = limit;
            result.RowCount = accounts.Count;
            result.PageCount = (int)Math.Ceiling(accounts.Count * 1.0 / limit);
            result.Queryable = accounts.Skip((page-1)*limit).Take(limit).AsQueryable();
            return result;
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> AccountDel(string ids)
        {
            await _repository.DeleteAsync(x=> ids.Contains(x.Id.ToString()));
            return 1;
        }

        /// <summary>
        /// 获取单个用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<AccountUpdDto> GetSingAccount(int id)
        {
            Account account = await _repository.FirstOrDefaultAsync(x=>x.Id == id);
            return ObjectMapper.Map<Account, AccountUpdDto>(account);
        }

        /// <summary>
        /// 编辑用户信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<int> AccountUpd(AccountUpdDto dto)
        {
            var accountlist = await _repository.GetListAsync(l=>l.Id != dto.Id && l.AccountName == dto.AccountName);
            if (accountlist.Count>0)
            {
                return -1;
            }
            Account account = ObjectMapper.Map<AccountUpdDto, Account>(dto);
            Account accountup = await _repository.UpdateAsync(account);
            return accountup == null ? 0 :1;
        }
    }
}
